% Compute second part of Table 2 in Ritschl, Sarferaz, and Uebele (2015)
clear all
clc

addpath('funcs');
addpath('scrpts');
%% Prepare data and results
% See run_table1.m

% Define different periods
tinterval.ft_preWW1 = 1:47;
tinterval.ft_pre30  = 1:63;
tinterval.ft_14to29 = 48:63;
period = {'preWW1','14to29','pre30'};
samples = {'52'; '98'};
save('matFiles/tinterval','period','tinterval','samples');

%% Load results and compute standard deviations for Table 2 II 52 series
clear all; clc;
% Define subsets
subset_load= {'all'};
%tv_spec98 = {'intermediate';'constant'};
tv_spec = {'intermediate';'constant'};
%tv_spec2 = {'intermediate';'tight_71_01'};
save('matFiles/subset_load','subset_load');
save('matFiles/tv_spec','tv_spec');

for px=1:size(tv_spec,1)
    save('matFiles/px','px');
    load('matFiles/subset_load');
    
    for ix =1:size(subset_load,1)
        load('matFiles/px');
        load('matFiles/tv_spec');
        load('matFiles/tinterval');
        load('matFiles/subset_load');
        load('data/data_gdp');
        filename_52 = char(['matFiles/' tv_spec{px} '/results_52_1867_2006_',subset_load{ix},'_',tv_spec{px}]);
        load(filename_52);
        ft_52 = results.ft(:,1:63,:);
        clear results.ft
        
        filename_98 = char(['matFiles/' tv_spec{px} '/results_US_98_1867_1929', '_',tv_spec{px}]);
        load(filename_98);
        ft_98 = results.ft(:,1:63,:);
        
        
        ft_stand_52 = ft_52*std_bg./repmat(std(ft_52,0,2), [1,size(ft_52,2),1]);
        ft_stand_98 = ft_98*std_bg./repmat(std(ft_98,0,2), [1,size(ft_98,2),1]);
        
        %     for px=1:size(period,2)-1 % without preWW2
        %         eval(['ft_all.' subset_load{ix} '_' period{px} '_52' '=ft_stand_52(:,tinterval.ft_' period{px} ',:);']);
        %         eval(['ft_all.' subset_load{ix} '_' period{px} '_98' '=ft_stand_98(:,tinterval.ft_' period{px} ',:);']);
        %     end
        
        for sx = 1:size(samples,1)
            eval(['perstd.' subset_load{ix} '_' period{1} '_'  tv_spec{px} '_med_' samples{sx} '= median(std(ft_stand_' samples{sx} '(:,tinterval.ft_' period{1} ',:),0,2),3);']);
            eval(['perstd.' subset_load{ix} '_' period{1} '_'  tv_spec{px} '_std_' samples{sx} '= std(std(ft_stand_' samples{sx} '(:,tinterval.ft_' period{1} ',:),0,2),0,3);']);
            
            eval(['perstd.' subset_load{ix} '_' period{2} '_'  tv_spec{px} '_med_'  samples{sx} '= median(std(ft_stand_' samples{sx} '(:,tinterval.ft_' period{2} ',:),0,2),3);']);
            eval(['perstd.' subset_load{ix} '_' period{2} '_'  tv_spec{px} '_std_' samples{sx} '= std(std(ft_stand_' samples{sx} '(:,tinterval.ft_' period{2} ',:),0,2),0,3);']);
            
            
            eval(['temp=std(ft_stand_' samples{sx} '(:,tinterval.ft_' period{2} ',:),0,2)./std(ft_stand_' samples{sx} '(:,tinterval.ft_' period{1} ',:),0,2);']);
            eval(['perstd.' subset_load{ix} '_'  tv_spec{px}  '_14to29preWW1_med_' samples{sx} '= median(temp,3);']);
            eval(['perstd.' subset_load{ix} '_'  tv_spec{px}  '_14to29preWW1_std_' samples{sx} '= std(temp,0,3);']);
        end
        
        save('matFiles/factors_table2_II','perstd');%'ft_all',
        
        clear all;
        load('matFiles/factors_table2_II');
    end
    
end
%% Create Excel-file of results
load('matFiles/subset_load');
load('matFiles/factors_table2_II');

perstd_c = struct2cell(perstd);
for ix=1:size(subset_load,1)*2
    perstd_mat(ix,:) = cell2mat(perstd_c(1+12*(ix-1):12+12*(ix-1)))'.*[ones(1,4)*100, ones(1,2),ones(1,4)*100, ones(1,2)];
end


names_col = {'','medPreWW1_52','stdPreWW1_52','med14to29_52','std14to29_52',...
    'med14to29preWW1_52','std14to29preWW1_52','medPreWW1_98',...
    'stdPreWW1_98','med14to29_98','std14to29_98','med14to29preWW1_98','std14to29preWW1_98'};
names_row = [subset_load;subset_load];

xlswrite('tables/volaComparison_final', names_col, 'Table2_II_fromMatlab', 'A1');
xlswrite('tables/volaComparison_final', names_row, 'Table2_II_fromMatlab', 'A2');
xlswrite('tables/volaComparison_final', perstd_mat, 'Table2_II_fromMatlab', 'B2');
